package com.pingan.internal.point.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.pingan.internal.point.shopping.common.msg.ErrorMessage;
import com.pingan.internal.point.shopping.common.utils.FileUtils;
import com.pingan.internal.point.shopping.controller.form.GoodsDeliveryForm;
import com.pingan.internal.point.shopping.controller.form.GoodsFinishOrder;
import com.pingan.internal.point.shopping.controller.form.GoodsSaveForm;
import com.pingan.internal.point.shopping.controller.form.GoodsWithdrawForm;
import com.pingan.internal.point.shopping.controller.response.ResponseData;
import com.pingan.internal.point.shopping.service.GoodsOrderService;
import com.pingan.internal.point.shopping.service.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

@Controller
@RequestMapping("admin/goods")
public class GoodsAdminController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsAdminController.class);

    @Resource
    private GoodsService goodsService;

    @Resource
    private GoodsOrderService goodsOrderService;


    @Value("${point.goods.upload.path}")
    private String path;

    @PutMapping("save")
    @PreAuthorize("hasAnyRole('RP_ADMIN','CP_ADMIN')")
    public @ResponseBody
    ResponseData save(@RequestBody GoodsSaveForm form){
        LOGGER.info("GoodsAdminController_save_form:{}", JSON.toJSONString(form));
        goodsService.save(form);
        LOGGER.info("GoodsAdminController_save_end");
        return ResponseData.success();
    }

    @PostMapping("update")
    @PreAuthorize("hasAnyRole('RP_ADMIN','CP_ADMIN')")
    public @ResponseBody
    ResponseData update(@RequestBody GoodsSaveForm form){
        LOGGER.info("GoodsAdminController_update_form:{}", JSON.toJSONString(form));
        goodsService.update(form);
        LOGGER.info("GoodsAdminController_update_end");
        return ResponseData.success();
    }

    @PostMapping("save-image")
    public @ResponseBody
    ResponseData saveImage(@RequestParam("goodsImagesName") MultipartFile goodsImages){
        LOGGER.info("GoodsAdminController_save_form start");
        String uuid = UUID.randomUUID().toString();
        String fileName = goodsImages.getOriginalFilename();
        String fullFileName = path+uuid +fileName;
        FileUtils.upload(goodsImages,fullFileName);
        LOGGER.info("GoodsAdminController_save_end");
        return ResponseData.success(uuid +fileName);
    }

    @PostMapping("deliver-goods")
    @PreAuthorize("hasAnyRole('RP_ADMIN','CP_ADMIN')")
    @ResponseBody
    public ResponseData deliverGoods(@RequestBody GoodsDeliveryForm form) {
        String result = goodsOrderService.deliverOrder(form);
        if (result == null) {
            ResponseData.error(ErrorMessage.DELIVERY_ERROR);
        }
        return ResponseData.success(result);
    }

    @PostMapping("finish")
    @PreAuthorize("hasAnyRole('RP_ADMIN','CP_ADMIN')")
    @ResponseBody
    public ResponseData finishOrder(@RequestBody GoodsFinishOrder form) {
        String result =goodsOrderService.finishOrder(form);
        return ResponseData.success(result);
    }

    @PostMapping("withdraw")
    @PreAuthorize("hasAnyRole('RP_ADMIN','CP_ADMIN')")
    @ResponseBody
    public ResponseData withdrawOrder(@RequestBody GoodsWithdrawForm form) {
        String result =goodsOrderService.withdrawOrder(form);
        return ResponseData.success(result);
    }
}
